package com.keqiang.chart.impl.axis.impl.combined

import android.graphics.Canvas
import android.graphics.Paint
import android.text.TextPaint
import com.keqiang.chart.impl.axis.render.YAxisRender
import com.keqiang.chart.utils.ObjectPool

/**
 * 组合图Y轴绘制器
 *
 * @author Created by wanggaowan on 2023/4/20 09:07
 */
class CombinedYAxisRender<T : CombinedChart>(chart: T, drawLabelByGridLine: Boolean = true) : YAxisRender<T>(chart, drawLabelByGridLine) {

    override fun onDraw(canvas: Canvas, start: Float, top: Float, end: Float, bottom: Float, labelMaxWidth: Float, gridHeight: Float, xAxisRange: Float) {
        if (chart.mainChart == ChartType.BAR) {
            chart.barChar.yAxisRender.onDraw(canvas, start, top, end, bottom, labelMaxWidth, gridHeight, xAxisRange)
        } else {
            chart.lineChar.yAxisRender.onDraw(canvas, start, top, end, bottom, labelMaxWidth, gridHeight, xAxisRange)
        }
    }

    /**
     * 绘制图表右边轴线图
     */
    fun onDrawEnd(canvas: Canvas, start: Float, top: Float, end: Float, bottom: Float, labelMaxWidth: Float, gridHeight: Float, xAxisRange: Float) {
        // 绘制轴线
        val yAxis = chart.yAxisEnd ?: return

        val paint = ObjectPool.obtainPaint<Paint>()
        val textPaint = ObjectPool.obtainPaint<TextPaint>(true)
        val tempRectF = ObjectPool.obtainRectF()
        val path = ObjectPool.obtainPath()

        val xAxis = chart.xAxis
        setPaint(paint, yAxis.lineColor, yAxis.lineWidth, yAxis.linePathEffect)
        val x = start + (end - start) / 2f
        path.moveTo(x, top)
        path.lineTo(x, bottom)
        canvas.drawPath(path, paint)
        yAxis.drawRectF.set(start, top, end, bottom)

        // 绘制y轴文本
        var tempBottom = bottom - yAxis.startPadding
        textPaint.textSize = yAxis.labelTextSize
        textPaint.color = yAxis.labelColor
        setPaint(paint, yAxis.gridLineColor, yAxis.gridLineWidth, yAxis.gridLinePathEffect)

        yAxis.labelList?.let {
            for (index in it.indices) {
                if (labelMaxWidth > 0 && gridHeight > 0) {
                    if (drawLabelByGridLine) {
                        drawLabelByGridLine(canvas, textPaint, index, it, yAxis, gridHeight, end,
                            labelMaxWidth, tempBottom, tempRectF, true)
                    } else {
                        drawLabelByGridCenter(canvas, textPaint, index, it, yAxis, gridHeight, end,
                            labelMaxWidth, tempBottom, tempRectF, true)
                    }
                }

                if (index > 0 && yAxis.drawGridLine) {
                    var startX =
                        if (yAxis.gridUseStartPadding) end - xAxisRange + xAxis.startPadding else end - xAxisRange
                    if (xAxis.drawGridLine && index == it.size - 1) {
                        startX -= xAxis.gridLineWidth / 2
                    }

                    path.reset()
                    path.moveTo(if (yAxis.gridUseEndPadding) end - xAxis.endPadding else end, tempBottom)
                    path.lineTo(startX, tempBottom)
                    canvas.drawPath(path, paint)
                }

                tempBottom -= gridHeight
            }

            if (!drawLabelByGridLine && yAxis.drawGridLine) {
                var stopX =
                    if (yAxis.gridUseEndPadding) start + xAxisRange - xAxis.endPadding else start + xAxisRange
                if (xAxis.drawGridLine) {
                    // 由Y轴补全X轴Y轴交叉点缺失部分，如果yAxis.gridUseEndPadding为true，则Y轴结束点实际都在X轴最后一根轴线左边
                    stopX += xAxis.gridLineWidth / 2
                }

                path.reset()
                path.moveTo(if (yAxis.gridUseStartPadding) start + xAxis.startPadding else start, tempBottom)
                path.lineTo(stopX, tempBottom)
                canvas.drawPath(path, paint)
            }
        }

        ObjectPool.recycleRectF(tempRectF)
        ObjectPool.recyclePaint(paint)
        ObjectPool.recyclePaint(textPaint)
        ObjectPool.recyclePath(path)
    }
}
